#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#




import wsgiref.handlers
import os

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext import db
from google.appengine.api import users

class TempRecord(db.Model):
	user = db.UserProperty()
	feeling = db.StringProperty()
	day = db.DateTimeProperty(auto_now=True,auto_now_add=True)
	temp = db.FloatProperty()

class RecordTempHandler(webapp.RequestHandler):
	def get(self):
		user = users.get_current_user()
		if user:
			dayTemp = TempRecord()
			dayTemp.user = users.get_current_user()
			dayTemp.title = 'record'
			dayTemp.temp = eval(self.request.get('dayTemp'))
			dayTemp.put()
			self.redirect("/")
		else:
			self.redirect(users.create_login_url(self.request.uri))
			
			
class MainHandler(webapp.RequestHandler):
  def get(self):
	user = users.get_current_user()
	
	if user:
		q = db.GqlQuery("SELECT * FROM TempRecord WHERE user = :1 ORDER BY day DESC",user)
		templateValues = {"dayTemps": q,}
		path = os.path.join(os.path.dirname(__file__), 'FusionTest.html')
		self.response.out.write(template.render(path, templateValues))
		self.response.headers.add_header("content-type", "text/html")
	else:
		self.redirect(users.create_login_url(self.request.uri))

def main():
  application = webapp.WSGIApplication([('/', MainHandler),
										('/add',RecordTempHandler)],
                                       debug=True)
  wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
  main()
